package whackEm.V2;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class GameTimer {
	
	long gameTimeInMilis = 0;
	long previousTick = 0;
	long oneSecond = 1000;
	public boolean decreasingTime = true;
	private Bitmap stopWatchSprite;
	float leftStopwatchPos =  160;
	float topStopwatchPos =  30;
	float stopwatchWidth = 35;
	float stopwatchHeight = 37;
	float TimerHeight =  0;
	float TimerWidth=  0;
	float leftTimerPos =  0;
	float topTimerPos =  0;
	float paintSize = 37;
	private Paint paint = new Paint();
	int mins = 0;
	int secs = 0;
	WhackEmPanel panel;
	
//TODO colocar o paint do timer		
	 
	/*Salvar o estado do timer na pausa
	 * Terminar o jogo quando o timer chegar a 0
	 *
	 *  Adicionar tela de fim de jogo, pode ser igual a tela de pause.
	 */
	private boolean Paused = false;
	
	public GameTimer(Context c, long remainingTime,float scale,WhackEmPanel panel) {
         if (remainingTime != 0)
        	 gameTimeInMilis = remainingTime;
         else 
        	 decreasingTime = false;
         this.panel = panel;
         stopWatchSprite = BitmapFactory.decodeResource(c.getResources(), R.drawable.stopwatch);
         leftStopwatchPos *=  scale;
         topStopwatchPos *=  scale;
         stopwatchWidth *= scale;	
         stopwatchHeight *= scale;
         leftTimerPos = leftStopwatchPos + stopwatchWidth;
         topTimerPos =  topStopwatchPos + stopwatchHeight;
         paint.setTextSize(paintSize*scale);
         paint.setARGB(220, 59, 243, 234);
         
	}
	
	public void update(long gameTime ){
		
		if (!isPaused() ) { 
		 long time = (System.currentTimeMillis() - previousTick );    
		 if (time > oneSecond) {
			 if (decreasingTime)
				 gameTimeInMilis -= oneSecond;
			 else 
				 gameTimeInMilis += oneSecond;
			 previousTick = System.currentTimeMillis();
			 if (gameTimeInMilis == 0)
				 panel.gameEndedOnTimerOrLives(true);
		 }
		}
	}
	
	public String parseTime(long time){
		
		mins = (int) time / 1000 / 60;		
		secs = (int) time /1000 % 60;
		
		if (secs < 10) { 
			return (mins + ":0" + secs);
		}		
		return (mins + ":" + secs);
				
		
	}

	
	public void drawTimer(Canvas canvas){
		 canvas.drawBitmap(stopWatchSprite, leftStopwatchPos, topStopwatchPos, null);
		 canvas.drawText(parseTime(gameTimeInMilis), leftTimerPos,topTimerPos , paint);
	}
	
	public void increaseTimer(float timeToAddInMilis) { 
		gameTimeInMilis += timeToAddInMilis;
	} 
	
	public void decreaseTimer(float timeToReduceInMilis) {
		gameTimeInMilis += timeToReduceInMilis;
	} 

	/**
	 * @return the paused
	 */
	public boolean isPaused() {
		return Paused;
	}

	/**
	 * @param paused the paused to set
	 */
	public void pauseTimer(boolean paused) {
		Paused = paused;
	}

}
